package com.aco.party.service.system;

import com.aco.party.dao.custom.PostsDao;
import com.aco.party.dao.custom.TermsDao;
import com.aco.party.dao.custom.UserDao;
import com.aco.party.enums.VisitTypeEnum;
import com.aco.party.model.po.Posts;
import com.aco.party.model.po.Terms;
import com.aco.party.model.vo.PostsParam;
import com.aco.party.model.vo.PostsVo;
import com.aco.party.model.vo.VisitParam;
import com.aco.party.model.vo.common.PageParam;
import com.aco.party.model.vo.common.UserWithRoleVo;
import com.aco.party.util.ConfigResource;
import com.aco.party.util.WebModelUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by flying on 3/10/2016 0010.
 */
@Service
public class PostsService {
    @Resource
    private PostsDao postsDao;

    @Resource
    private VisitService visitService;

    @Resource
    private UserDao userDao;

    @Resource
    private TermsDao termsDao;

    public List<PostsVo> getByPage(PageParam pageParam,PostsParam param) {
        List<PostsVo> list = new ArrayList<>();
        List<Posts> postses = postsDao.getList(pageParam,param);
        PostsVo vo;
        if (postses != null) {
            for (Posts posts : postses) {
                vo = new PostsVo();
                BeanUtils.copyProperties(posts, vo);
                if(!StringUtils.isEmpty(posts.getExtra3())){
                    vo.setView(Long.parseLong(posts.getExtra3()));
                }else{
                    vo.setView(0L);
                }

                if(vo.getPostAuthor() != null){
                    UserWithRoleVo user = userDao.getUserWithRole(vo.getPostAuthor());
                    if(user != null) {
                        vo.setPostAuthorStr(user.getNickName());
                        vo.setAuthorPhoto(user.getPhotoUrl());
                    }
                }
                VisitParam visitParam = new VisitParam();
                visitParam.setType(VisitTypeEnum.文章.getValue());
                visitParam.setTargetId(posts.getId());
                vo.setView(visitService.getCountByParam(visitParam).longValue());
                if(vo.getPostTerm() != null){
                    Terms terms = termsDao.getById(vo.getPostTerm());
                    vo.setPostTermsStr(terms.getName());
                }
                list.add(vo);
            }
        }
        return list;
    }

    public Integer getTotal(PostsParam param) {
        return postsDao.getTotal(param);
    }

    public void update(Posts posts) {
        posts.setUpdateTime(new Date());
        postsDao.updateByPrimaryKeySelective(posts);
    }

    public void add(Posts posts) {
        posts.setCreateTime(new Date());
        postsDao.insertSelective(posts);
    }

    public void delete(Long id) {
        postsDao.deleteByPrimaryKey(id);
    }

    public Posts getByPrimaryKey(Long id) {
        return postsDao.selectByPrimaryKey(id);
    }

    public PostsVo getVoById(Long id){
        PostsVo vo = new PostsVo();
        Posts posts = postsDao.selectByPrimaryKey(id);
        if(posts != null){
            if(!StringUtils.isEmpty(posts.getExtra3())){
                vo.setView(Long.parseLong(posts.getExtra3()));
            }else{
                vo.setView(0L);
            }
            if(StringUtils.isNoneEmpty(posts.getPostContent())){
                posts.setPostContent(posts.getPostContent().replace("${base}", WebModelUtil.getBasePath()));
            }
            BeanUtils.copyProperties(posts,vo);
            if(vo.getPostAuthor() != null){
                UserWithRoleVo user = userDao.getUserWithRole(vo.getPostAuthor());
                if(user != null) {
                    vo.setPostAuthorStr(user.getNickName());
                    vo.setAuthorPhoto(user.getPhotoUrl());
                    vo.setRoleName(user.getRoleName());
                }
            }
        }
        return vo;
    }

    public List<Long> getIdsByParam(PostsParam param){
        return postsDao.getIdsByParam(param);
    }
}
